package com.csi.jf.crucible.api.crucible;

import com.chinasofti.ro.bizframework.modules.json.JSONArray;
import com.chinasofti.ro.bizframework.modules.json.JSONObject;

public class ReviewModel {
	
	private JSONObject instance = new JSONObject();
	
	public ReviewModel(String projectKey, String name, String description
			,JSONObject author, JSONObject moderator, JSONObject creator
			,JSONObject permaId, JSONObject changesets) {
		this.projectKey = projectKey;
		this.name = name;
		this.description = description;
		this.author = author;
		this.moderator = moderator;
		this.creator = creator;
		this.permaId = permaId;
		this.changesets = changesets;
	}
	
	private String projectKey;
	private String name;
	private String description;
	
	/**
	 * "author" : {
	 * "userName" : "joe",
	 * "displayName" : "Joe Krustofski",
	 * "avatarUrl" : "http://foo.com/avatar"
	 * },
	 */
	private JSONObject author;
	
	/**
	 * "moderator" : {
	 * "userName" : "scott",
	 * "displayName" : "Scott the Moderator",
	 * "avatarUrl" : "http://foo.com/avatar"
	 * },
	 */
	private JSONObject moderator;
	
	/**
	 * "creator" : {
	 * "userName" : "joe",
	 * "displayName" : "Joe Krustofski",
	 * "avatarUrl" : "http://foo.com/avatar"
	 * },
	 */
	private JSONObject creator;
	
	/**
	 * "permaId" : {
	 * "id" : "CR-FOO-21"
	 * },
	 */
	private JSONObject permaId;
	
	/**
	 *    "\"changesets\" : {"+
	 *      "\"changesetData\" : [ ],"+//{\"id\" : \"63452\"}
	 *      "\"repository\" : \"CrucibleTest\""+
	 *    "}"+
	 */
	private JSONObject changesets;
	
	
	public String createReviewModel() {
		
		JSONObject reviewData = new JSONObject();
		reviewData.put("projectKey", projectKey);
		reviewData.put("name", name);
		reviewData.put("description", description);
		reviewData.put("author", author);
		reviewData.put("moderator", moderator);
		reviewData.put("creator", creator);
		reviewData.put("permaId", permaId);
		JSONArray ja = new JSONArray();
		if(permaId == null || !permaId.containsKey("id")) {
			reviewData.put("permaIdHistory", ja);
		} else {
			ja.add(permaId.get("id"));
			reviewData.put("permaIdHistory", ja);
		}
		reviewData.put("type", "REVIEW");
		reviewData.put("allowReviewersToJoin", Boolean.TRUE);
		reviewData.put("metricsVersion", 4);
		reviewData.put("createDate", "2014-05-06T15:15:23.922+0200");
		reviewData.put("dueDate", "2014-05-07T15:15:23.922+0200");
		reviewData.put("jiraIssueKey", "");
		
		instance.put("reviewData", reviewData);
		instance.put("changesets", changesets);
		
		return instance.toString();
	}
	
}
